﻿using System;
using System.Net.Mail;
using INHollandair.Core.Models;
using INHollandair.Core.Services;

namespace INHollandair.Account
{
    public partial class ForgotPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pnl_ForgotPassword.Visible = false;
            pnl_ForgotPasswordSubmitted.Visible = false;

            if (!IsPostBack)
            {
                Session["CaptchaImageText"] = RngUtils.GetRandomNumberAsString(6);
            }

            if (Session["ForgotPasswordSubmitted"] != null)
            {
                pnl_ForgotPasswordSubmitted.Visible = true;
            }
            else
            {
                pnl_ForgotPassword.Visible = true;
                EmailAddress.Focus();
                pnl_ForgotPassword.DefaultButton = "ForgotPasswordButton";
            }
        }

        protected void ForgotPasswordClicked(object sender, EventArgs e)
        {
            string captcha = Server.HtmlEncode(txt_Captcha.Text);
            if (captcha.Equals(Session["CaptchaImageText"].ToString()))
            {
                UserService svc = new UserService();
                MailAddress mail = null;
                bool mailValid = true;

                try
                {
                    mail = new MailAddress(Server.HtmlEncode(EmailAddress.Text));
                }
                catch (Exception)
                {
                    mailValid = false;
                }

                if (mailValid)
                {
                    bool mailExists = svc.UserEmailExists(mail.Address);
                    if (mailExists)
                    {
                        string url = RngUtils.GetUrlString();

                        User user = svc.GetUserByEmailAddress(mail.Address);
                        ResetPasswordURL resetPasswordUrl = new ResetPasswordURL
                        {
                            UserId = user.Id,
                            ResetPasswordUrl = url,
                            DateGenerated = DateTime.Now
                        };

                        ResetPasswordURL possibleOldUrl = svc.GetResetPasswordUrlByUser(user);
                        if(possibleOldUrl != null)
                        {
                            resetPasswordUrl.Id = possibleOldUrl.Id;
                            svc.UpdateResetPasswordUrl(resetPasswordUrl);
                        }
                        else
                        {
                            svc.InsertResetPasswordUrl(resetPasswordUrl);    
                        }

                        Mailer mailer = new Mailer();
                        mailer.SendForgotPasswordMail(user, resetPasswordUrl);

                        Session["ForgotPasswordSubmitted"] = true;
                        Response.Redirect("~/Account/ForgotPassword.aspx");
                    }
                    else
                    {
                        errorLabel.Text = "E-mail address is unknown.";
                    }
                }
                else
                {
                    errorLabel.Text = "E-mail address is invalid.";
                }
            }
            else
            {
                errorLabel.Text = "Captcha input did not match.";
            }
        }
    }
}